package com.lijin.study.java.other;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class InvokeShell {

	public static void execShell(String shell){  
		try {
			Runtime rt = Runtime.getRuntime();
			rt.exec(shell);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static List<String> runShell(String shStr) throws Exception {
		List<String> strList = new ArrayList<String>();
		
		Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);
		InputStreamReader ir = new InputStreamReader(process.getInputStream());
		LineNumberReader input = new LineNumberReader(ir);
		String line;
		process.waitFor();
		while ((line = input.readLine()) != null){
			strList.add(line);
		}
		
		return strList;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println("执行开始");
		List<String> rls = runShell(args[0]);
		for (int i = 0; i < rls.size(); i ++) {
			System.out.println(rls.get(i));
		}
		System.out.println("执行结束");
	}
}
